/*
 * @Author: your name
 * @Date: 2024-04-19 10:00:57
 * @LastEditTime: 2024-04-21 10:34:36
 * @LastEditors: Please set LastEditors
 * @Description: In User Settings Edit
 * @FilePath: \.leetcode\111.二叉树的最小深度.cpp
 */
/*
 * @lc app=leetcode.cn id=111 lang=cpp
 *
 * [111] 二叉树的最小深度
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * struct TreeNode {
 *     int val;
 *     TreeNode *left;
 *     TreeNode *right;
 *     TreeNode() : val(0), left(nullptr), right(nullptr) {}
 *     TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
 *     TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
 * };
 */
class Solution {
public:
    // int minDepth(TreeNode* root) {
    //     if(root == nullptr){
    //         return 0;
    //     }
    //     queue<TreeNode*> que;
    //     int depth = 0;
    //     int minx = INT_MAX;
    //     que.push(root);
    //     while(!que.empty()){
    //         int size = que.size();
    //         depth++;
    //         for(int i = 0; i < size; i++){
    //             TreeNode* tmp_node = que.front();
    //             que.pop();
    //             if(tmp_node->left) que.push(tmp_node->left);
    //             if(tmp_node->right) que.push(tmp_node->right);
    //             if(!tmp_node->left && !tmp_node->right){
    //                 // cout<< depth<<endl;
    //                 minx = min(minx, depth);
    //             }
    //         }
    //     }
    //     return minx;
    // }
    int getDepth(TreeNode* root){
        if(!root){
            return 0;
        }
        int left = getDepth(root->left);
        int right = getDepth(root->right);
        if(!root->left && root->right){
            return 1+right;
        }
        else if(!root->right && root->left){
            return 1+left;
        }
        else{
            return 1+min(left,right);
        }
    }
    int minDepth(TreeNode* root) {

        return getDepth(root);
    }

};
// @lc code=end

